
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PlayBoardPrueba.java
 *
 * Created on Jan 10, 2012, 4:08:48 PM
 */
/**
 *
 * @author Figueroa
 */
public class PlayBoardPrueba extends javax.swing.JApplet implements MouseListener, MouseMotionListener {
    private String SelectedPiece;

    /** Initializes the applet PlayBoardPrueba */
    @Override
    public void init() {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(PlayBoardPrueba.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(PlayBoardPrueba.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(PlayBoardPrueba.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(PlayBoardPrueba.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

      
                    initComponents();
                    
             this.setSize(1210, 510);
             resize(size().width < 200 ? 200 : size().width,
	     size().height < 200 ? 200 : size().height);
                Graphics g=getGraphics();
        interfaz(g);
     
    }
    
    private PlayBoard tablero;
  
    public void interfaz(Graphics g){
    
     
      tablero = new PlayBoard();
      tablero.setSize(450, 450);
       tablero.addMouseListener(this);
      tablero.addMouseMotionListener(this);
  
      tablero.paint(g);
    
   
  
      
     PanelBoard.add( tablero, null);
     PanelBoard.setSize(tablero.getSize());
         
    
   
  
    }
    
    
   
   

  
    @Override
    public void mouseClicked(MouseEvent e) {
       if(e.getSource() == tablero){
       Graphics g=this.getGraphics();
           if(SelectedPiece !=null){
              int x = e.getX();
              int y = e.getY();
              int i=tablero.posinv(x);
              int j=tablero.posinv(y);
             
              if(!SelectedPiece.equals("Clean")){
                    tablero.piece[j*8+i]= tablero.get_piece(SelectedPiece);
                 }
              else{
                 tablero.piece[j*8+i] =' ';
              }
              
              tablero.update(g);
             
           }
         /*  if(e.getButton()==MouseEvent.BUTTON3){
              int x = e.getX();
              int y = e.getY();
              int i=tablero.posinv(x);
              int j=tablero.posinv(y);
              Graphics gr = tablero.getGraphics();
           if(tablero.piece[j*8+i] == ' ' ){
     gr.setColor(Color.YELLOW);
     gr.fillRect(tablero.pos(i),tablero.pos(j),tablero.width,tablero.width);
           }
       }
          if(e.getButton() == MouseEvent.BUTTON1){
               Graphics gr = tablero.getGraphics();
              int x = e.getX();
              int y = e.getY();
              int i=tablero.posinv(x);
              int j=tablero.posinv(y);
              if(tablero.piece[j*8+i] == ' ' ){
           if (tablero.isDark(i,j)) {
           gr.setColor(tablero.darkSquare);
        } else {
           gr.setColor(tablero.lightSquare); 
        } 
     
    
     gr.fillRect(tablero.pos(i),tablero.pos(j),tablero.width,tablero.width);
          
          }*/}
    
    
    }

    @Override
    public void mousePressed(MouseEvent e) {
  
              
        if( e.getSource() == tablero){
         Graphics g=this.getGraphics();
         int x = e.getX();
         int y = e.getY();
         int i=tablero.posinv(x);
         int j=tablero.posinv(y);
         System.out.println("I"+i+"J"+j);
      if (i>=8 || j>=8) {
         return;
      }
      //  do nothing if square is empty
      if (tablero.piece[j*8+i]==' ') {
         return;
      }
      //  do nothing if square is not one of the
      //  pieces of the color whose turn it is
     if (tablero.whiteTurn!=tablero.isWhite(tablero.piece[j*8+i])) {
         return;
      }
      
      // start dragging a piece      
      tablero.dragging=true;
      tablero.temp=false;
      tablero.dragCol=i;
      tablero.dragRow=j;
      tablero.dragPiece=tablero.piece[j*8+i];
      // highlight the selected square in red
      tablero.drawSquare(g,i,j,true);
      return;
    }
    
    }

    @Override
    public void mouseReleased(MouseEvent e) {
         if( e.getSource() == tablero){
     Graphics g=this.getGraphics();
     int x = e.getX();
     int y = e.getY(); 
     int i=tablero.posinv(x);
      int j=tablero.posinv(y);
      if (tablero.dragging) {
         if (tablero.temp && i>=0 && j<8
                  && !(i==tablero.dragCol && j==tablero.dragRow)) {
            // make the temporary move permanent
            tablero.drawSquare(g,tablero.tempCol,tablero.tempRow,false);
            // remove the piece from its old position
            tablero.piece[tablero.dragRow*8+tablero.dragCol]=' ';
             // place piece in its new position
            tablero.piece[j*8+i]=tablero.dragPiece;
            tablero.drawSquare(g,i,j,false);
            if(!tablero.whiteTurn){
              Registry +=","+tablero.translate_move(tablero.dragRow, tablero.dragCol, j, i, tablero.dragPiece)+"\n";
            }
            else{
                Registry += ""+tablero.translate_move(tablero.dragRow, tablero.dragCol, j, i, tablero.dragPiece);
            }
            Registrytxt.setText(Registry);
            // change the turn
          tablero.whiteTurn=!tablero.whiteTurn;
          
         }
         // remove highlighting on old position
         tablero.drawSquare(g,tablero.dragCol,tablero.dragRow,false);
         tablero.temp=false;
         tablero.dragging=false;
      }
      return;
    }
    }

    @Override
    public void mouseEntered(MouseEvent e) {
     //
    }

    @Override
    public void mouseExited(MouseEvent e) {
        //Do nothing
    }

    @Override
    public void mouseDragged(MouseEvent e) {
         if( e.getSource() == tablero){
   //  System.out.print("Mouse dragged");
        Graphics g=this.getGraphics();
       int x = e.getX();
       int y = e.getY();
       int i=tablero.posinv(x);
       int j=tablero.posinv(y);
       if (tablero.dragging) {
         // has nothing changed from last time?
         if (tablero.temp && i==tablero.tempCol && j==tablero.tempRow) {
            return ;
         }
         if (tablero.temp) {
            // clear the previous temporary move
            tablero.drawSquare(g,tablero.tempCol,tablero.tempRow,false);
            tablero.temp=false;
         }
         // check for legal move 
         if (tablero.legalMove(i,j)) {
            // update new position to show the piece
            tablero.temp=true;
            tablero.tempCol=i;
            tablero.tempRow=j;
            tablero.drawSquare(g,i,j,true);
         }
      }
      return ;
    }
    }

    @Override
    public void mouseMoved(MouseEvent e) {
       // throw new UnsupportedOperationException("Not supported yet.");
    }

    /** This method is called from within the init() method to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        PanelBoard = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        Registrytxt = new javax.swing.JTextArea();

        getContentPane().setLayout(new java.awt.GridLayout(1, 0));

        Registrytxt.setColumns(20);
        Registrytxt.setRows(5);
        jScrollPane1.setViewportView(Registrytxt);

        javax.swing.GroupLayout PanelBoardLayout = new javax.swing.GroupLayout(PanelBoard);
        PanelBoard.setLayout(PanelBoardLayout);
        PanelBoardLayout.setHorizontalGroup(
            PanelBoardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(PanelBoardLayout.createSequentialGroup()
                .addContainerGap(716, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        PanelBoardLayout.setVerticalGroup(
            PanelBoardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(PanelBoardLayout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 312, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(87, Short.MAX_VALUE))
        );

        getContentPane().add(PanelBoard);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel PanelBoard;
    private javax.swing.JTextArea Registrytxt;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
    public String Registry="";
}
